package com.whmall.service.crm.provider;

import com.whmall.component.common.ObjectConverter;
import com.whmall.service.crm.api.CompanyContactApi;
import com.whmall.service.crm.entity.dto.companycontact.CompanyContactRspDTO;
import com.whmall.service.crm.entity.dto.companycontact.CreateOrUpdateCompanyContactReqDTO;
import com.whmall.service.crm.module.companycontact.CompanyContactService;
import com.whmall.service.crm.module.companycontact.entity.CompanyContactDO;
import org.apache.dubbo.config.annotation.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * CompanyContactProvider
 *
 * @author alex
 * @since 2020/8/5
 */
@Service(interfaceClass = CompanyContactApi.class)
public class CompanyContactProvider implements CompanyContactApi {
    private final CompanyContactService companyContactService;
    private final ObjectConverter<CompanyContactDO, CompanyContactRspDTO> do2dtoConverter;

    public CompanyContactProvider(CompanyContactService companyContactService,
                                  ObjectConverter<CompanyContactDO, CompanyContactRspDTO> do2dtoConverter) {
        this.companyContactService = companyContactService;
        this.do2dtoConverter = do2dtoConverter;
    }

    @Override
    public void createCompanyContact(Long requesterId, Long companyId, CreateOrUpdateCompanyContactReqDTO request) {
        companyContactService.createCompanyContact(requesterId, companyId, request);
    }

    @Override
    public void updateCompanyContact(Long requesterId, Long companyId, Long companyContactId, CreateOrUpdateCompanyContactReqDTO request) {
        companyContactService.updateCompanyContact(requesterId, companyId, companyContactId, request);
    }

    @Override
    public void removeCompanyContact(Long requesterId, Long companyId, Long companyContactId) {
        companyContactService.removeCompanyContact(requesterId, companyId, companyContactId);
    }

    @Override
    public CompanyContactRspDTO getByCompanyIdAndCompanyContactId(Long companyId, Long companyContactId) {
        return Optional.ofNullable(companyContactService.getByCompanyIdAndCompanyContactId(companyId, companyContactId))
                .map(do2dtoConverter::convert)
                .orElse(null);
    }

    @Override
    public List<CompanyContactRspDTO> listByCompanyId(Long companyId) {
        return companyContactService.listByCompanyId(companyId)
                .stream()
                .map(do2dtoConverter::convert)
                .collect(Collectors.toList());
    }
}
